// house_pkg/pages/locate/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 高德地图api的key
    key:'db5a9598ecf970651d4ed6a6c3036b11',
    // 当前位置
    Location:'',
    //当前经纬度
    longlatude:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    await this.getLocation()
    await this.getOtherplace()
  },

  /**
   * 获取当前位置
   */
  async getLocation(){
    const res = await wx.getLocation({
      altitude:true,
      isHighAccuracy:true
    })
    // console.log(res);
    //保存经纬度
    this.setData({
      longlatude:`${res.longitude},${res.latitude}`
    })
    //逆地理编码
    //当前位置
    let nowLocation = ''
   wx.request({
      url: 'https://restapi.amap.com/v3/geocode/regeo',
      data:{
        key:this.data.key,
        location:`${res.longitude},${res.latitude}`
      },
      success(data){
        // console.log(data);
        // 设置当前地点
      nowLocation = data.data.regeocode.addressComponent.streetNumber.street+data.data.regeocode.addressComponent.streetNumber.number
      // console.log(nowLocation);
      }
    })
    setTimeout(() => {
      this.setData({
        Location:nowLocation
      })
    }, 3000);
  },

  //获取周边小区
  async getOtherplace(){
    //获取周边小区信息
   const res = await new Promise((reslove,reject)=>{
      wx.request({
        url: 'https://restapi.amap.com/v3/place/around',
        data:{
          key:this.data.key,
          location:this.data.longlatude,
          keywords:'小区',
          radius:500
        },
        success(res){
          reslove(res)
        }
      })
    })
    console.log(res);
    //遍历拿到的信息数组,取出里面的小区名字
    const Apartment = res.data.pois.map((item)=>{
      return item.name
    })
    this.setData({
      ApartmentList:Apartment
    })
    // console.log(this.data.ApartmentList);
  }
})